﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace FotoAnotSharp
{
    public partial class GateAnotationLabelsForm : Form
    {
        private Dictionary<CultureInfo, PairWrapper> mLabels = new Dictionary<CultureInfo, PairWrapper>();
        private FotoAnotSharp.GateAnotaionComponent.AnotatedObject anotatedObject;
        private BindingList<PairWrapper> mBindList = new BindingList<PairWrapper>();

        class PairWrapper
        {
            public CultureInfo Key { get; set; }
            public string Label { get; set; }

            static readonly string[] SEPARATOR = new string[] { "\n" };
            public override string ToString()
            {
                string[] splt =  Label.Split(SEPARATOR, 2, StringSplitOptions.RemoveEmptyEntries);
                return Key.DisplayName + ": " + splt[0] + (splt.Length > 1 ? "..." : "");
            }
        }

        public GateAnotationLabelsForm(FotoAnotSharp.GateAnotaionComponent.AnotatedObject obj)
        {
            InitializeComponent();

            anotatedObject = obj;
            listBoxAnotations.DataSource = mBindList;
            
            if (!string.IsNullOrEmpty(obj.Name))
                textBoxName.Text = obj.Name;
            
            foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.NeutralCultures))
            {
                comboBoxCulture.DisplayMember = "DisplayName";
                comboBoxCulture.Items.Add(info);
            }

            foreach (KeyValuePair<CultureInfo, string> pair in obj.Labels)
            {
                PairWrapper temp = new PairWrapper() { Key = pair.Key, Label = pair.Value };
                mLabels[pair.Key] = temp;
                mBindList.Add(temp);
            }

            Icon = Application.OpenForms[0].Icon;
        }

        void UpdateText()
        {
            this.Text = "Objekt: " + anotatedObject.DisplayName;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBoxCulture.SelectedItem == null || string.IsNullOrEmpty(textBoxAnotation.Text))
                return;

            CultureInfo info = (CultureInfo)comboBoxCulture.SelectedItem;
            PairWrapper value;

            if (!mLabels.TryGetValue(info, out value))
            {
                value = new PairWrapper() { Key = info, Label = textBoxAnotation.Text };
                mLabels[info] = value;
                mBindList.Add(value);
            }
            else
            {
                value.Label = textBoxAnotation.Text;
                mBindList.ResetBindings();
            }

            comboBoxCulture_SelectedIndexChanged(null, null);
        }

        private void comboBoxCulture_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxCulture.SelectedItem == null)
                return;

            PairWrapper wrap;
            if (mLabels.TryGetValue((CultureInfo)comboBoxCulture.SelectedItem, out wrap))
            {
                textBoxAnotation.Text = wrap.Label;
                buttonAdd.Text = "Upravit";
            }
            else
                buttonAdd.Text = "Přidat";
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Right) == System.Windows.Forms.MouseButtons.Right)
            {
                RemoveSelectedLabel();
            }
        }

        void RemoveSelectedLabel()
        {
            if (listBoxAnotations.SelectedItem == null)
                return;

            PairWrapper temp = (PairWrapper)listBoxAnotations.SelectedItem;
            mLabels.Remove(temp.Key);
            mBindList.Remove(temp);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            anotatedObject.ClearLabels();

            foreach (PairWrapper pair in mLabels.Values)
            {
                anotatedObject.AddLabel(pair.Key, pair.Label);
            }

            anotatedObject.Name = textBoxName.Text;

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxAnotations.SelectedItem == null)
                return;

            //KeyValuePair<CultureInfo, string> temp = (KeyValuePair<CultureInfo, string>)listBoxAnotations.SelectedItem;
            PairWrapper temp = (PairWrapper)listBoxAnotations.SelectedItem;
            comboBoxCulture.SelectedIndex = comboBoxCulture.Items.IndexOf(temp.Key);
            textBoxAnotation.Text = temp.Label;
        }


        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            RemoveSelectedLabel();
        }

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            UpdateText();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                buttonStorno.PerformClick();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
